package com.lzxuni.modules.qualitycomplaints;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.modules.common.service.FileEntityService;
import com.lzxuni.modules.qualitycomplaints.entity.QualityComplaints;
import com.lzxuni.modules.qualitycomplaints.service.QualityComplaintsService;
import com.lzxuni.modules.enterprise.entity.EnterpriseInfo;
import com.lzxuni.modules.enterprise.service.EnterpriseInfoService;
import com.lzxuni.modules.productdisplay.entity.ProductDisplay;
import com.lzxuni.modules.productdisplay.service.ProductDisplayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * 质量投诉 前端控制器
 * </p>
 *
 * @author 杨国营
 * @since 2019-07-09
 */
@RestController
@RequestMapping("/qualityComplaints")
public class QualityComplaintsController {
	//质量投诉
	@Autowired
	private QualityComplaintsService qualityComplaintsService;
	//产品
	@Autowired
	private ProductDisplayService productDisplayService;
	//企业
	@Autowired
	private EnterpriseInfoService enterpriseInfoService;
	//保存附件使用
	@Autowired
	private FileEntityService fileEntityService;


	@RequestMapping("/getInformation")
	public Object getInformation(String id) throws Exception {
		//根据产品id查询产品信息包括：企业名称（id），产品名称(id)，追溯码
		ProductDisplay productDisplay = productDisplayService.getOne(new QueryWrapper<ProductDisplay>().eq("YW_ID", id));
		if(productDisplay !=null){
			EnterpriseInfo enterpriseInfo = enterpriseInfoService.getOne(new QueryWrapper<EnterpriseInfo>().eq("YW_ID", productDisplay.getAffiliatedEnterprises()));
			if(enterpriseInfo !=null){
				productDisplay.setEnterpriseName(enterpriseInfo.getEnterpriseName());
			}
		}
		return R.ok().put("data", productDisplay);
	}
	@Transactional
	@RequestMapping("/saveForm")
	public Object saveForm(QualityComplaints qualityComplaints,String zltsfiles) throws Exception {
		//保存投诉信息
		String uuid = UuidUtil.get32UUID();
		qualityComplaints.setId(uuid);
		qualityComplaints.setComplainTime(new Date());
		qualityComplaints.setIsanswer("否");
		qualityComplaintsService.save(qualityComplaints);
		//保存附件信息到数据库
		fileEntityService.insert(zltsfiles, uuid, "ZLTSGL", "ZLTS", "质量投诉附件");

		return R.ok().put("data", "保存成功！");
	}



}

